package com.itxh.core.builder;

import com.itxh.core.config.Configuration;
import com.itxh.core.factory.DefaultSqlSessionFactory;
import com.itxh.core.factory.SqlSessionFactory;
import com.itxh.core.utils.DocumentUtils;
import org.dom4j.Document;

import java.io.InputStream;
import java.io.Reader;

public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream inputStream){
        Document document = DocumentUtils.createDocument(inputStream);
        XMLConfigBuilder configBuilder = new XMLConfigBuilder();
        Configuration configuration = configBuilder.parseConfiguration(document.getRootElement());

        return build(configuration);
    }

    public SqlSessionFactory build(Reader reader){
        Document document = DocumentUtils.createDocument(reader);
        XMLConfigBuilder configBuilder = new XMLConfigBuilder();
        Configuration configuration = configBuilder.parseConfiguration(document.getRootElement());

        return build(configuration);
    }

    private SqlSessionFactory build(Configuration configuration){
        return new DefaultSqlSessionFactory(configuration) ;
    }
}
